(function ($, window, undefined) {
  'use strict';

  var $doc = $(document),
      Modernizr = window.Modernizr;

  $(document).ready(function() {
    $.fn.foundationAlerts           ? $doc.foundationAlerts() : null;
    $.fn.foundationButtons          ? $doc.foundationButtons() : null;
    $.fn.foundationAccordion        ? $doc.foundationAccordion() : null;
    $.fn.foundationNavigation       ? $doc.foundationNavigation() : null;
    $.fn.foundationTopBar           ? $doc.foundationTopBar() : null;
    $.fn.foundationCustomForms      ? $doc.foundationCustomForms() : null;
    $.fn.foundationMediaQueryViewer ? $doc.foundationMediaQueryViewer() : null;
    $.fn.foundationTabs             ? $doc.foundationTabs({callback : $.foundation.customForms.appendCustomMarkup}) : null;
    $.fn.foundationTooltips         ? $doc.foundationTooltips() : null;
    $.fn.foundationMagellan         ? $doc.foundationMagellan() : null;
    $.fn.foundationClearing         ? $doc.foundationClearing() : null;

    $.fn.placeholder                ? $('input, textarea').placeholder() : null;
    
    
	
  });

  // UNCOMMENT THE LINE YOU WANT BELOW IF YOU WANT IE8 SUPPORT AND ARE USING .block-grids
  // $('.block-grid.two-up>li:nth-child(2n+1)').css({clear: 'both'});
  // $('.block-grid.three-up>li:nth-child(3n+1)').css({clear: 'both'});
  // $('.block-grid.four-up>li:nth-child(4n+1)').css({clear: 'both'});
  // $('.block-grid.five-up>li:nth-child(5n+1)').css({clear: 'both'});

  // Hide address bar on mobile devices (except if #hash present, so we don't mess up deep linking).
  if (Modernizr.touch && !window.location.hash) {
    $(window).load(function () {
      setTimeout(function () {
        window.scrollTo(0, 1);
      }, 0);
    });
  }
  
  function login(){
  		//check email
	  var email = $('#username').val();
	  var subpage = $('#slug_bad_sub').val();
	  
	  $('#form_manager_message').slideUp(300);
	  $('#form_manager_message').removeClass('serror').addClass('snormal');
	  $('#message_content').html('<li>'+JS_ALERT_LOADING+'</li>');
	  $('#form_manager_message').slideDown(300);
	
	  $.ajax({
		url: "index.php?option=com_wrd&task=profile.checkInputUserName",
		type: "POST",
		data: 'email='+email+'&ajax=1',
		success: function (res) {
			var obj = $.parseJSON(res);
			if(obj.status == 'error'){
	  			$('#form_manager_message').removeClass('snormal').addClass('serror');
				$('#message_content').html('<li>'+obj.msg+'</li>');
				$('#form_manager_message').slideDown(300).delay(3000).slideUp(300);
			}else{
				//check pass
		          var password = $('#password_login').val();
		          $.ajax({
		  			url: "index.php?option=com_wrd&task=profile.checkpass",
		  			type: "POST",
		  			data: 'password='+password+'&ajax=1&username='+obj.username,
		  			success: function (res) {
		  				var obj = jQuery.parseJSON(res);
		  				if(obj.status == 'error'){
	  						$('#form_manager_message').removeClass('snormal').addClass('serror');
		  					$('#message_content').html('<li>'+obj.msg+'</li>');
		  					$('#form_manager_message').slideDown(300);
		  					$('#form_manager_message').delay(3000).slideUp(300);
		  				}
		  				else{
		  					//proccess login
		  					var password = $('#password_login').val();
		  					jQuery.ajax({
		  						url: "index.php?option=com_wrd&task=profile.login",
		  						type: "POST",
		  						data: 'password='+password+'&username='+email+'&ajax=1',
		  						success: function (res) {
		  							var obj = jQuery.parseJSON(res);
		  							if(obj.status == 'error'){
	  									$('#form_manager_message').removeClass('snormal').addClass('serror');
		  								$('#message_content').html('<li">'+obj.msg+'</li>');
		  								$('#form_manager_message').slideDown(300);
		  								$('#form_manager_message').delay(3000).slideUp(300);
		  							}
		  							else{
		  								
										$(".linkMenuRight").html("");
										location.reload(true);
										
										if(subpage == 1){
		  									location.reload(true);
		  								}else{
		  									if(obj.redirect == 1)
		  										window.location.replace(base_url+"index.php?option=com_wrd&view=profile&layout=default&id=1&Itemid=170");

											$('#form_manager_message').slideUp(300);
											$('#message_content').html('<li></li>');
	  										
	  
			  								$('#login').hide();
			  								$('#space').hide();
			  								$('#signUp').hide();
			  								//console.log("Message response: "+obj.msg);
			  								$('ul.nav-bar .linkMenuRight li:nth-child(3)').after(obj.msg);
			  								$('.logo').css({'margin-top': '0px'});
			  								
			  								//clear popup
								       	 	$('.loginform').removeClass('loginform-1');
								     	 	$('.forlogin').slideUp('slow',function(){
								     	 		$('.blurbg').fadeOut();
								     	 		$('.menu-bg').removeClass('menu-bg-1');
								     	 		
								 				$('.forlogin').removeClass('reg-open');
								 				$('.forlogin').removeClass('login-open');
								     	 	});
							     	 	}
		  							}
		  						}
		  		  		   });
		  				}
		  			}
		  		   });
			}
		}
	   });
  }
  
  function openLogin(){
  	if($('.forlogin').hasClass('login-open')){
  	 	$('.logo').css({'margin-top': '0px'});
  	 	$('.loginform').removeClass('loginform-1');
	 	$('.forlogin').slideUp('slow',function(){
	 		$('.blurbg').fadeOut();
	 		$('.menu-bg').removeClass('menu-bg-1');
	 		$('.forlogin').removeClass('reg-open');
			$('.forlogin').removeClass('login-open');
	 	});
	 }else{
		$('#loginfrm').show();
		$('#regfrm').hide();
	 	$('.forlogin').slideDown('slow',function(){
	 		$('.loginform').addClass('loginform-1');
	 		$('.logo').css({'margin-top': '315px'});
	 		
	 	});
		$('.menu-bg').addClass('menu-bg-1');
		$('.forlogin').addClass('login-open');
		$('.forlogin').removeClass('reg-open');
		$('.blurbg').fadeIn();
	 }
  }
  
  $(window).load(function() {
	  //check link url
	  $(location).attr('href');
	  var hashkey = window.location.hash;
	  //console.log(pathname);
	  //console.log(window.location);
	  if(hashkey == '#registration'){
			$('#regfrm').show();
     		$('#loginfrm').hide();
     	 	$('.forlogin').slideDown('slow',function(){
     	 		$('.loginform').addClass('loginform-1');
     	 		$('.logo').css({'margin-top': '315px'});
     	 		
     	 	});
     		$('.menu-bg').addClass('menu-bg-1');
     		$('.forlogin').addClass('reg-open');
     		$('.forlogin').removeClass('login-open');
     		$('.blurbg').fadeIn();
	  }
	  else if(hashkey == '#login'){
	  		 openLogin();
	  }
	  //$('.serror1').delay(3000).slideUp(300);
	  
      $("#featured").orbit(
	  {
		  timer: false
	  });
      $(".sig-container").orbit(
	  {
		  timer: false
	  });
      
      $(".ads").orbit(
	  {
		  advanceSpeed: 10000, 	
		  animation: 'fade',
		  directionalNav: false
	  });
      

      $('.bar,.silde,.metro_1_col,.metro_2_col,.metro_3_col,.metro_5_col,.metro_6_col,.metro_8_col,.metro_9_col,.metro_10_col,.metro_12_col,.metro_13_col,.metro_14_col,.metro_15_col, .metro_16_col, .metro_17_col, .pre_footer_fisrt_col,.pre_footer_third_col,.pre_footer_second_col,.image_section').mosaic({
			animation	:	'slide'
		});
      
      $('#login').click(function(){
      	 openLogin();
      });
      
      $('.signUp,#signUp_alt').click(function(){
    	     
       	 if($('.forlogin').hasClass('reg-open')){
       		 
       	 	$('.logo').css({'margin-top': '0px'});
       	 	$('.loginform').removeClass('loginform-1');
     	 	$('.forlogin').slideUp('slow',function(){
     	 		$('.blurbg').fadeOut();
     	 		$('.menu-bg').removeClass('menu-bg-1');
     	 		
 				$('.forlogin').removeClass('reg-open');
 				$('.forlogin').removeClass('login-open');
     	 	});
     	 	
     	 }else{
     		$('#regfrm').show();
     		$('#loginfrm').hide();
     	 	$('.forlogin').slideDown('slow',function(){
     	 		$('.loginform').addClass('loginform-1');
     	 		$('.logo').css({'margin-top': '315px'});
     	 		
     	 	});
     		$('.menu-bg').addClass('menu-bg-1');
     		$('.forlogin').addClass('reg-open');
     		$('.forlogin').removeClass('login-open');
     		$('.blurbg').fadeIn();
     	 }
     	 
       });
      
     //=============================================
     //login
      $('#btn_login').click(function(){
    	  login();
      });
      
		$(window).keypress(function(e) {
	    	if(e.which == 13) {
	    		
	    		if($('.forlogin').hasClass('login-open')){
	    			login();
	    			
	    		}
	    	}
		});
      
     //=============================================
     //reg
     $('#signup').click(function(){
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,10})?$/;
		var email1 = $('#email1').val();
		var password = $('#password').val();
		var password1 = $('#password1').val();

		//check email
		if( !emailReg.test(email1) ) {
	  		$('#form_manager_message').removeClass('snormal').addClass('serror');
		  	$('#message_content').html('<li>'+JS_ALERT_EMAIL_IS_INVALID+'</li>');
		  	$('#form_manager_message').slideDown(300).delay(3000).slideUp(300);
		  	
		} else {
			var isChecked = $('#term').is(':checked');
			$('#form_manager_message').slideUp(300);
			$('#message_content').html('');
			
			//check password
			if(password == '' || password1 == ''){
				
	  			$('#form_manager_message').removeClass('snormal').addClass('serror');
		  		$('#message_content').html('<li>'+JS_ALERT_PASSWORD_EMPTY+'</li>');
		  		$('#form_manager_message').slideDown(300).delay(3000).slideUp(300);

			}
			else if(password != password1){
			
	  			$('#form_manager_message').removeClass('snormal').addClass('serror');
		  		$('#message_content').html('<li>'+JS_ALERT_THE_SAME_PASSWORD+'</li>');
		  		$('#form_manager_message').slideDown(300).delay(3000).slideUp(300);

			}
			else if(!isChecked) {
							
	  			$('#form_manager_message').removeClass('snormal').addClass('serror');
		  		$('#message_content').html('<li>'+JS_ALERT_PLEASE_TICK_CHECK_BOX+'</li>');
		  		$('#form_manager_message').slideDown(300).delay(3000).slideUp(300);
		  		
		  		
			}
			else{
				//check email is available
			
	  			$('#form_manager_message').removeClass('serror').addClass('snormal');
		  		$('#message_content').html('<li>'+JS_ALERT_LOADING+'</li>');
		  		$('#form_manager_message').slideDown(300).delay(3000).slideUp(300);
		  		
		  		
				jQuery.ajax({
					url: "index.php?option=com_wrd&task=profile.checkInputUserName",
					type: "POST",
					data: 'email='+email1+'&ajax=1',
					success: function (res) {
						var obj = jQuery.parseJSON(res);
						if(obj.status == 'error'){
							$('#regfrm').submit();
						}
						else{
							
	  						$('#form_manager_message').removeClass('snormal').addClass('serror');
		  					$('#message_content').html('<li>'+JS_ALERT_EMAIL_IS_NOT_AVAILABLE+'</li>');
		  					$('#form_manager_message').slideDown(300).delay(3000).slideUp(300);
						}
					}
	  		   });
			}
		}
     });
     
     //=============================================================
     $('.need_login').click(function(){
    	  openLogin();
     });
     
     $('.blurbg').click(function(){
     	$('.logo').css({'margin-top': '0px'});
   	 	$('.loginform').removeClass('loginform-1');
 	 	$('.forlogin').slideUp('slow',function(){
 	 		$('.blurbg').fadeOut();
 	 		$('.menu-bg').removeClass('menu-bg-1');
 	 		
			$('.forlogin').removeClass('reg-open');
			$('.forlogin').removeClass('login-open');
 	 	});
     });
     
     //==============================================================
     $('.way_check').click(function(){
     	//console.log($(this));
     	 if( $(this).is(":checked") ){ // check if the radio is checked
            var val = $(this).val(); // retrieve the value
           if(val == 2){
         		$('.pointonly').hide();
         		$('.point_cash').show();
           }
           else{
           		$('.pointonly').show();
         		$('.point_cash').hide();
           }
        }
     });

	//=====================================================================
	$('.expand').click(function(){
		if(!$(this).hasClass('colpand')){
			var curid = $(this).attr('id');
			$('.sub'+curid).slideDown();
			$(this).addClass('colpand');
			$(this).html('<img src="images/icon_collapse.gif">');
		}
		else{
			var curid = $(this).attr('id');
			$('.sub'+curid).slideUp();
			$(this).removeClass('colpand');
			$(this).html('<img src="images/icon_expand.gif">');
		}
	});
	
	//=======================================================================
  	$.urlParam = function getUrlVar(key){
		var result = new RegExp(key + "=([^&]*)", "i").exec(window.location.search); 
		return result && result[1] || ""; 
	};
	
	$.urlActive = function activeLink(name){
		$('.menuitem a').removeClass('menuactive');
		$('.l_'+name).addClass('menuactive');
		
	};
	
	var cur_id = decodeURIComponent($.urlParam('id').split(':')[1]);
	
	switch (cur_id){
		case 'undefined':
			$.urlActive('recommend');
			break;
		default:
			$.urlActive(cur_id);
			break;
	}
	
	$("img.lazy").lazyload();
	$("img.sig-image").lazyload();
  });
  
  
})(jQuery, this);


